function debounce(executor, wait, immediate) {
  let timeout = null;

  return function () {
    let _this = this;
    let args = arguments;
    clearTimeout(timeout);
    if (immediate) {
      const callNow = !timeout;
      timeout = setTimeout(() => {
        timeout = null;
      }, wait);
      if (callNow) executor.apply(_this.args);
    } else {
      timeout = setTimeout(() => {
        executor.apply(_this, args);
      }, wait);
    }
  };
}

export default debounce;
